import { makeAutoObservable } from 'mobx';
import { Permission,PermissionFormValues} from './permissionTypes';
import { getPermissionList, createPermission, updatePermission, getInfo, deletePermission ,getActiveRole, getActiveMenuTree} from '../../../../../apis/backhub/permission';

class PermissionStore {
  permissions: Permission[] = [];
  modalVisible = false; // 弹窗显示状态
  editingPermission: Permission | undefined = undefined; // 当前编辑的角色，undefined表示新建
  loading = false;
  roleValues:any;
  menuTree:[];
  roleOptions = [];

  constructor() {
    makeAutoObservable(this);
  }

  // 获取角色列表
  fetchPermissions = async () => {
    this.loading = true;
    try {
      const response = await getPermissionList();
      this.permissions = (response as any).data;
    } catch (error) {
      console.error('获取角色列表失败:', error);
    } finally {
      this.loading = false;
    }
  };
  
  // 获取用户
  getPermission = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingPermission =  (response as any).data;
      return this.editingPermission; // 返回获取到的用户数据
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };
  
  // 获取角色列表
  getActiveRole = async () => {
    this.loading = true;
    try {
      this.roleOptions = [];
      const response = await getActiveRole();
      this.roleValues = (response as any).data;
      this.roleValues && Object.entries(this.roleValues).forEach(([roleId, config]) => {
      this.roleOptions.push({
          ...config as any, 
          key: roleId 
        });
      });
    } catch (error) {
      console.error('获取用户列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 获取菜单树形列表
  getActiveMenuTree = async () => {
    this.loading = true;
    try {
      const response = await getActiveMenuTree();
      this.menuTree = (response as any).data;
    } catch (error) {
      console.error('获取用户列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingPermission = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (func: Permission) => {
    this.editingPermission = func;
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingPermission = undefined;
  };

  // 保存角色
  savePermission = async (values: Permission) => {
    try {
      if (this.editingPermission && this.editingPermission.id) {
        await updatePermission(this.editingPermission.id, values);
      } else {
        await createPermission(values);
      }
      this.hideModal();
      this.fetchPermissions();
    } catch (error) {
      console.error('保存角色失败:', error);
    }
  };


  // 删除角色
  deletePermission = async (id: string) => {
    try {
      await deletePermission(id);
      this.fetchPermissions();
    } catch (error) {
      console.error('删除角色失败:', error);
    }
  };
}

export const permissionStore = new PermissionStore();